package com.bingfor.easycablelink.account;

import android.databinding.ObservableInt;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;

import com.bingfor.easycablelink.R;
import com.bingfor.easycablelink.account.viewmodel.MainViewModel;
import com.bingfor.easycablelink.add.AddPage;
import com.bingfor.easycablelink.base.BaseActivity;
import com.bingfor.easycablelink.base.BaseFragment;
import com.bingfor.easycablelink.databinding.ActivityMainBinding;
import com.bingfor.easycablelink.home.HomePage;
import com.bingfor.easycablelink.my.MyPage;

public class MainActivity extends BaseActivity {

    public ActivityMainBinding binding;
    public BaseFragment[] fragments;
    private int lastPosition;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        if (savedInstanceState == null) {
            HomePage homePage = HomePage.getInstance();
            AddPage addPage = AddPage.getInstance();
            MyPage myPage = MyPage.getInstance();
            fragments = new BaseFragment[]{homePage, addPage, myPage};
            getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.contentPanel, homePage, HomePage.class.getName())
                    .add(R.id.contentPanel, addPage, AddPage.class.getName())
                    .add(R.id.contentPanel, myPage, MyPage.class.getName())
                    .hide(addPage)
                    .hide(myPage)
                    .show(homePage)
                    .commit();
        } else {
            HomePage mainPage = (HomePage) getSupportFragmentManager().findFragmentByTag(HomePage.class.getName());
            AddPage timeLineMainPage = (AddPage) getSupportFragmentManager().findFragmentByTag(AddPage.class.getName());
            MyPage orderListPage = (MyPage) getSupportFragmentManager().findFragmentByTag(MyPage.class.getName());
            fragments = new BaseFragment[]{mainPage, timeLineMainPage};
            getSupportFragmentManager()
                    .beginTransaction()
                    .hide(timeLineMainPage)
                    .hide(orderListPage)
                    .show(mainPage)
                    .commit();
        }
        initViews();
    }

    @Override
    public void initViews() {
        binding.setViewModel(new MainViewModel(mContext));
        final ObservableInt position =  binding.getViewModel().getPosition();
        position.addOnPropertyChangedCallback(new android.databinding.Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(android.databinding.Observable observable, int i) {

                getSupportFragmentManager()
                        .beginTransaction()
                        .hide(fragments[lastPosition])
                        .show(fragments[position.get()])
                        .commit();
                lastPosition = position.get();
            }
        });
    }

    @Override
    public void initToolbar() {

    }

    @Override
    public void refresh() {

    }
}
